import { _decorator, Component, Label, Node, Sprite, SpriteFrame, tween, Vec3 } from 'cc';
import { ProjectEventManager, ProjectEvent } from 'db://assets/Scripts/Framework/Managers/ProjectEventManager';
import { EasingType } from 'db://assets/Scripts/Framework/Utils/TweenUtil';
import { MainUI } from './MainUI';
import Banner from 'db://assets/Scripts/Banner';
import { BundleManager } from 'db://assets/Scripts/Framework/Managers/BundleManager';
import { GameDataManager } from '../GameDataManager';
const { ccclass, property } = _decorator;

@ccclass('RookiePackUI')
export class RookiePackUI extends Component {

    protected onEnable(): void {
        tween(this.node.children[1])
            .to(0.5, { scale: Vec3.ONE }, { easing: EasingType.backOut })
            .start();
        ProjectEventManager.emit(ProjectEvent.弹出窗口);
    }

    start() {

    }

    update(deltaTime: number) {

    }

    Back() {
        tween(this.node.children[1])
            .to(0.5, { scale: Vec3.ZERO }, { easing: EasingType.backIn })
            .call(() => { this.node.active = false; })
            .start();
        MainUI.Instance.PlayClick();
    }

    Video() {
        let x = this;
        Banner.Instance.ShowVideoAd(() => {
            x.Back();
            let arr = GameDataManager.Instance.getArrayData<number[]>("AllSkin");
            arr[2][3] = 1;
            GameDataManager.Instance.setArrayData("AllSkin", arr);
            arr = GameDataManager.Instance.getArrayData("SkinPieces");
            arr[2][3] = 20;
            GameDataManager.Instance.setArrayData("SkinPieces", arr);
            let data = MainUI.Instance.items.Adorn[2][3];
            let type = 3;
            let finish = MainUI.Instance.finish;
            for (let i = 0; i < finish.children[2].children.length; i++) {
                const element = finish.children[2].children[i];
                element.active = type == i;
            }
            BundleManager.GetBundle("MainPack").load(data.spriteFrame, SpriteFrame, (err, data) => { finish.children[2].children[type].getComponent(Sprite).spriteFrame = data; });
            let label = finish.children[3].getComponent(Label);
            label.string = data.name;
            tween(MainUI.Instance.finish)
                .to(0.5, { scale: Vec3.ONE }, { easing: EasingType.backOut })
                .start();
            MainUI.Instance.PlayBonus();
            MainUI.Instance._InitPack();
        });
    }

}


